package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.R;
import com.example.demo.common.pageResult;
import com.example.demo.entiy.Category;
import com.example.demo.service.CategoryServiceImp;
import com.example.demo.service.SetmeaiServiceImp;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryServiceImp categoryServiceImp;
    @Autowired
    SetmeaiServiceImp setmeaiServiceImp;
    @GetMapping("/page")
    public R<Object> getCateInfo(int page, int pageSize){
        pageResult<Category> page1 = categoryServiceImp.getInfo(page,pageSize);
        return R.success(JSON.toJSON(page1));
    }
    @PostMapping
    public R<String> putCategory(HttpServletRequest request, @RequestBody Category category){
        Boolean b = categoryServiceImp.putCategory(request, category);
        if (b) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    @PutMapping
    public R<String> changeCateInfo(HttpServletRequest request,@RequestBody Category category){
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        Boolean change = categoryServiceImp.changeCateInfo(category);
        if (change){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }
    @DeleteMapping
    public R<String> deleteInfo(Long ids){

        boolean idex = categoryServiceImp.deletInfo(ids);
        if (!idex){
            return R.success("删除成功");
        }
        return R.error("该类型下还有套餐");
    }
    @GetMapping("/list")
    public R<List<Category>> getList(int type){
        List<Category> category =categoryServiceImp.getName(type);
        log.info("list:{}",category);
        return R.success(category);
    }
}
